﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.WindowsAPICodePack.ApplicationServices;

namespace BaseLibrary.Power
{
    public class PowerWatcher : UserControl
    {
        public PowerWatcher()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                StackPanel sp = new StackPanel();
                sp.Background = new SolidColorBrush(Colors.Red);

                TextBlock tb = new TextBlock();
                tb.Text = "[ Power Watcher ]";
                sp.Children.Add(tb);

                this.Content = sp;
            }

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            PowerManager.PowerPersonalityChanged += (s, e) =>
            {
                if (PowerPersonalityChanged != null)
                {
                    PowerPersonalityChanged(this, new PowerEventArgs());
                }
            };

            // Änderung der Stromquelle
            PowerManager.PowerSourceChanged += (s, e) =>
            {
                if (PowerSourceChanged != null)
                {
                    PowerSourceChanged(this, new PowerEventArgs());
                }
            };

            // Änderung des Batterieladezustands
            PowerManager.BatteryLifePercentChanged += (s, e) =>
            {
                if (BatteryLifePercentChanged != null)
                {
                    BatteryLifePercentChanged(this, new PowerEventArgs());
                }
            };

            this.Loaded += (s, e) =>
                {
                    // Trick to init
                    if (BatteryLifePercentChanged != null)
                    {
                        BatteryLifePercentChanged(this, new PowerEventArgs());
                    }
                };
        }

        public event EventHandler<PowerEventArgs> BatteryLifePercentChanged;
        public event EventHandler<PowerEventArgs> PowerSourceChanged;
        public event EventHandler<PowerEventArgs> PowerPersonalityChanged;

        private void CheckPowerStatus()
        {

        }
    }
}
